﻿/*
 * 版 本 Learun-ADMS V7.0.3 敏捷开发框架
 * Copyright (c) 2015-2020 Hexagon PPM
 * 创建人：前端开发组
 * 日 期：2017.05.24
 * 描 述：lr-uploader 表单附件选择插件
 */
(function ($, learun) {
    "use strict";

    $.lrUploader = {
        init: function ($self) {
            var dfop = $self[0]._lrUploader.dfop;
            $.lrUploader.initRender($self, dfop);
        },
        initRender: function ($self, dfop) {
            $self.attr('type', 'lr-Uploader').addClass('lrUploader-wrap');
            var $wrap = $('<div class="lrUploader-input" ></div>');

            var $btnGroup = $('<div class="lrUploader-btn-group"></div>');
            var $uploadBtn = $('<a id="lrUploader_uploadBtn_' + dfop.id + '" class="btn btn-success lrUploader-input-btn">上传</a>');
            var $downBtn = $('<a id="lrUploader_downBtn_' + dfop.id + '" onmouseover="this.style.backgroundColor=\'#2E8B57\'" onmouseout="this.style.backgroundColor=\'#3CB371\';"  style="color: #fff;background-color:#3CB371;border-color: #ffffff;border: 1px solid transparent;border-radius: 4px;" class="btn btn-danger lrUploader-input-btn">下载</a>');

            $self.append($wrap);
            if (!dfop.isUpload) {
                $uploadBtn.attr("disabled", true);
            }
            if (!dfop.isDown) {
                $downBtn.attr("disabled", true);
            }
            $btnGroup.append($uploadBtn);
            $btnGroup.append($downBtn);
            $self.append($btnGroup);

            $uploadBtn.on('click', $.lrUploader.openUploadForm);
            $downBtn.on('click', $.lrUploader.openDownForm);
        },
        openUploadForm: function () {
            var $btn = $(this);
            var $self = $btn.parents('.lrUploader-wrap');
            var dfop = $self[0]._lrUploader.dfop;
            if (dfop.id == "ReportFileID" && dfop.type == "edit") {
                learun.layerConfirm('是否要覆盖之前上传的文件？', function (res) {
                    if (res) {
                        $.lrUploader.openUpload(dfop);
                    }
                });
            } else {
                $.lrUploader.openUpload(dfop);
            }
            
        },
        openUpload: function (df) {
            var dfop = df;
            learun.layerForm({
                id: dfop.id,
                title: dfop.placeholder,
                url: top.$.rootUrl + '/LR_SystemModule/Annexes/UploadForm?keyVaule=' + dfop.value + "&extensions=" + dfop.extensions + "&filePath=" + dfop.filePath + "&multiple=" + dfop.multiple + "&format=" + dfop.format + "&uploadTable=" + dfop.id,
                width: 600,
                height: 400,
                maxmin: true,
                btn: ['确认'],
                callBack: function (id) {
                    if (top["layer_" + dfop.id].upload == 1) {
                        return;
                    }
                    else {
                        if (dfop.callback) {
                            typeof dfop.callback === 'function' && dfop.callback(dfop);
                            learun.layerClose(dfop.id);
                        }
                        learun.httpAsyncGet(top.$.rootUrl + '/LR_SystemModule/Annexes/GetFileNames?folderId=' + dfop.value, function (res) {
                            if (res.code == learun.httpCode.success) {
                                $('#' + dfop.id).find('.lrUploader-input').text(res.info);
                            } 
                        });

                    }
                },
                end: function () {

                }
            });
        },
        openDownForm: function () {
            var $btn = $(this);
            var $self = $btn.parents('.lrUploader-wrap');
            var dfop = $self[0]._lrUploader.dfop;
            learun.layerForm({
                id: dfop.id,
                title: dfop.placeholder,
                url: top.$.rootUrl + '/LR_SystemModule/Annexes/DownForm?keyVaule=' + dfop.value,
                width: 600,
                height: 400,
                maxmin: true,
                btn: null
            });
        },
        openViewForm: function () {
            var $btn = $(this);
            var $self = $btn.parents('.lrUploader-wrap');
            var dfop = $self[0]._lrUploader.dfop;
            learun.layerForm({
                id: dfop.id,
                title: dfop.placeholder,
                url: top.$.rootUrl + '/LR_SystemModule/Annexes/DownForm?keyVaule=' + dfop.value + "&openForm=View",
                width: 600,
                height: 400,
                maxmin: true,
                btn: null
            });

            //var $btn = $(this);
            //var $self = $btn.parents('.lrUploader-wrap');
            //var dfop = $self[0]._lrUploader.dfop;
            //learun.layerForm({
            //    id: 'PreviewForm',
            //    title: '文件预览',
            //    url: top.$.rootUrl + '/LR_SystemModule/Annexes/PreviewFile?fileId=' + dfop.value,
            //    width: 1080,
            //    height: 850,
            //    btn: null
            //});
        },
        ViewModelFn: function () {
            //模型审核
            learun.layerForm({
                id: 'form',
                title: '模型审核',
                url: top.$.rootUrl + '/GZDT_BDPM/Model/ModelApproval?BimModelApprovalId=' + processId + "&ModelCode=" + ModelCode + "&ModelName=" + ModelName + "&CreateDate=" + CreateDate + "&StationId=" + StationId + "&BimModelFileId=" + BimModelFileId,
                width: 1200,
                height: 700,
                btn: null,
                maxmin: true,
                end: function () {
                    var param = { BimModelApprovalId: processId }
                    $('#BDPM_TJHQ_BimModelApprovalOpinion').jfGridSet('reload', { queryJson: JSON.stringify(param) });
                }
            });
        }
    };

    $.fn.lrUploader = function (op) {
        var $this = $(this);
        if (!!$this[0]._lrUploader) {
            return $this;
        }
        var dfop = {
            placeholder: '附件管理',
            type: '',
            isUpload: true,
            isDown: true,
            multiple: true,
            format: '',
            extensions: '',
            value: learun.newGuid(),
            filePath: ''//上传路径（配置文件）
        }

        $.extend(dfop, op || {});
        dfop.id = $this.attr('id');
        $this[0]._lrUploader = { dfop: dfop };
        $.lrUploader.init($this);
    };

    $.fn.lrUploaderSet = function (value) {
        if (value == null || value == 'null' || value == undefined || value == 'undefined' || value == '') {
            return;
        }

        var $self = $(this);
        var dfop = $self[0]._lrUploader.dfop;
        dfop.value = value;
        learun.httpAsyncGet(top.$.rootUrl + '/LR_SystemModule/Annexes/GetFileNames?folderId=' + dfop.value, function (res) {
            if (res.code == learun.httpCode.success) {
                $('#' + dfop.id).find('.lrUploader-input').text(res.info);
            }
        });
    }

    $.fn.lrUploaderGet = function () {
        var $this = $(this);
        var dfop = $this[0]._lrUploader.dfop;
        return dfop.value;
    }
})(jQuery, top.learun);